package cc.comac.mom.service;


import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.event.Level;

import cc.comac.mom.bean.BusLog;
import cc.comac.mom.queue.BusLogQueue;

public class MomLoggerFactory implements MomLogger {

	private final Logger logger;
	
	private String category;

	// 以下为自定义的日志级别
	//public static final Level PAY_LEVEL = CustomerLevel.BUSINESS;


	private MomLoggerFactory(Class<?> clazz) {
		logger = LoggerFactory.getLogger(clazz);
		category = "BUSINESS";
	}
	
	private MomLoggerFactory(Class<?> clazz,String category) {
		logger = LoggerFactory.getLogger(clazz);
		this.category = category;
	}

	public static MomLoggerFactory getLogger(final Class<?> clazz) {
		return new MomLoggerFactory(clazz);
	}
	
	public static MomLoggerFactory getLogger(final Class<?> clazz,String category) {
		return new MomLoggerFactory(clazz,category);
	}
	
	public void info(String message) {
		logger.info(message);
		
	}

	public void info(String message, Throwable t) {
		logger.info(message, t);
	}

	public void debug(String message) {
		logger.debug(message);
	}

	public void debug(String message, Throwable t) {
		logger.debug(message, t);
	}

	public void warn(String message) {
		logger.warn(message);
	}

	public void warn(String message, Throwable t) {
		logger.warn(message, t);
	}

	public void error(String message) {
		logger.error(message);
	}

	public void error(String message, Throwable t) {
		logger.error(message, t);
	}

	public void trace(String message) {
		logger.trace(message);
	}

	public void trace(String message, Throwable t) {
		logger.trace(message, t);
	}

	public void info(String message, Object... params) {
		logger.info(message, params);
	}

	public void debug(String message, Object... params) {
		logger.debug(message, params);
	}

	public void warn(String message, Object... params) {
		logger.warn(message, params);
	}

	public void error(String message, Object... params) {
		logger.error(message, params);
	}

	public void trace(String message, Object... params) {
		logger.trace(message, params);
	}
	
	private BusLog getLogInfoBean(String tableName, Level level, String message, String remark, String serviceName,
			String cate){
		
		BusLog info = new BusLog();
		if(tableName!=null) {
			info.setTableName(tableName);
		}
		if(level!=null) {
			info.setLevel(level.toString());
		}else {
			info.setLevel("INFO");
		}
		if(message!=null) {
			info.setMsg(message);
		}
		if(remark!=null) {
			info.setRemark(remark);
		}
		if(category!=null) {
			info.setCategory(cate);
		}else {
			info.setCategory(category);
		}
		if(serviceName!=null) {
			info.setServiceName(serviceName);
		}else {
//			info.setServiceName(ConfigUtil.getValue("spring.application.name"));
		}
		info.setCreationTime(LocalDateTime.now());
		info.setUserName(MDC.get("userName"));
		info.setClientIp(MDC.get("clientIP"));
		
		return info;
	}

	@Override
	public void cacheLog(String message) {
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(null,null,message,null,null,null));
	}

	@Override
	public void cacheLog(String tableName, String message) {
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(tableName,null,message,null,null,null));
	}

	@Override
	public void cacheLog(Level level, String message) {
		//String tableName, Level level, String message, String remark, String serviceName,String category
		level = level==null?Level.INFO:level;
		BusLogQueue.getInstance().push(getLogInfoBean(null,level,message,null,null,null));
	}

	@Override
	public void cacheLog(String tableName, Level level, String message) {
		//String tableName, Level level, String message, String remark, String serviceName,String category
		level = level==null?Level.INFO:level;
		BusLogQueue.getInstance().push(getLogInfoBean(tableName,level,message,null,null,null));
	}
	
	@Override
	public void cacheLog(Level level, String message, String keyword) {
		level = level==null?Level.INFO:level;
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(null,level,message,keyword,null,null));
	}
	
	@Override
	public void cacheLog(String tableName, Level level, String message, String keyword) {
		level = level==null?Level.INFO:level;
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(tableName,level,message,keyword,null,null));
	}

	@Override
	public void cacheLog(Level level, String message, String keyword, String serviceName, String category) {
		level = level==null?Level.INFO:level;
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(null,level,message,keyword,serviceName,category));
	}

	@Override
	public void cacheLog(String tableName, Level level, String message, String keyword, String serviceName,
			String category) {
		level = level==null?Level.INFO:level;
		//String tableName, Level level, String message, String remark, String serviceName,String category
		BusLogQueue.getInstance().push(getLogInfoBean(tableName,level,message,keyword,serviceName,category));
	}

}
